package action.cart;

import java.util.ArrayList;
import java.util.List;
import service.CartService;
import service.impl.CartFactory;
import util.CookieUtil;
import entity.CartItem;
import entity.User;
import action.BaseAction;

public class CartAction extends BaseAction{
	//input
	private int id;
	private int qty;
	//output
	private double cost;
	private double cutPrice;
	private List<CartItem> items = new ArrayList<CartItem>();
	private List<CartItem> delItems = new ArrayList<CartItem>();//删除的列表
	//injection
	private CartService cartService;
	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}
	
	public String list() throws Exception{
		items = cartService.getProds(true);
		delItems=cartService.getProds(false);
		cost=cartService.cost();
		cutPrice=cartService.cutPrice();
		return "success";
	}
	
	public String del() throws Exception{
		System.out.println("del");
		cartService.del(id);
		CookieUtil.addCookie(getCookieName(), cartService.store(), httpRes);
		return "list";
	}
	
	public String modify() throws Exception{
		cartService.modify(id,qty);
		CookieUtil.addCookie(getCookieName(), cartService.store(), httpRes);
		return "list";
	}
	
	public String recover() throws Exception{
		cartService.recover(id);
		CookieUtil.addCookie(getCookieName(), cartService.store(), httpRes);
		return "list";
	}
	
	private String getCookieName(){
		User user = (User)session.get("user");
		return user.getId()+"";
	}
	
	public List<CartItem> getItems() {
		return items;
	}
	public void setItems(List<CartItem> items) {
		this.items = items;
	}
	public double getCost() {
		return cost;
	}
	public void setCost(double cost) {
		this.cost = cost;
	}
	public double getCutPrice() {
		return cutPrice;
	}
	public void setCutPrice(double cutPrice) {
		this.cutPrice = cutPrice;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public List<CartItem> getDelItems() {
		return delItems;
	}

	public void setDelItems(List<CartItem> delItems) {
		this.delItems = delItems;
	}
	
}
